package thread;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-11
 * Time：16:34
 */
class Count {
    private final Object locker = new Object();
    public int sum;
    synchronized public void increase() {
        sum++;
    }
}

public class Demo12 {
    public static void main(String[] args) {
        Count count1 = new Count();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count1.increase();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count1.increase();
            }
        });

        Count count2 = new Count();
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count2.increase();
            }
        });

        t1.start();
        t2.start();
        t3.start();

        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(count1.sum);
        System.out.println(count2.sum);
    }
}
